package com.situ.controller;

import com.situ.bean.Category;

import com.situ.service.impl.AdminCategoryService;
import com.situ.service.impl.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/categories")
public class AdminCategoryController {

    @Autowired
    private AdminCategoryService categoryService;

    // 获取所有商品种类
    @GetMapping
    public List<Category> getAllCategories() {
        return categoryService.getAllCategories();
    }

    // 根据ID获取商品种类
    @GetMapping("/{id}")
    public Category getCategoryById(@PathVariable int id) {
        return categoryService.getCategoryById(id);
    }

    // 添加商品种类
    @PostMapping
    public String addCategory(@RequestBody Category category) {
        categoryService.addCategory(category);
        return "商品种类添加成功";
    }

    // 更新商品种类
    @PutMapping("/{id}")
    public String updateCategory(@PathVariable int id, @RequestBody Category category) {
        category.setId(id); // 确保ID一致
        categoryService.updateCategory(category);
        return "商品种类更新成功";
    }

    // 删除商品种类
    @DeleteMapping("/{id}")
    public String deleteCategory(@PathVariable int id) {
        categoryService.deleteCategory(id);
        return "商品种类删除成功";
    }

    // 获取所有父级分类
    @GetMapping("/parent")
    public List<Category> getAllParentCategories() {
        return categoryService.getAllParentCategories();
    }

    // 根据父级ID获取子分类
    @GetMapping("/parent/{pid}")
    public List<Category> getCategoriesByParentId(@PathVariable int pid) {
        return categoryService.getCategoriesByParentId(pid);
    }
}